package com.dys.entity;

import com.alibaba.fastjson.annotation.JSONField;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @Author: 疾风
 * @ClassName: LoginUser
 * @Description: 用户详情
 * @CreateDate: 2024/12/13
 * @Version: v1.0.0
 */
public class LoginUser implements UserDetails {
    private SysUser sysUser;
    /**
     * 存储权限的集合
     */
    private List<String> permissions;

    /**
     * 存储角色信息的集合
     */
    private List<String> roles;

    /**
     * authorities集合不需要序列化,只需要序列化permissions集合即可
     */
    @JSONField(serialize = false)
    private List<SimpleGrantedAuthority> authorities;

    public LoginUser() {
    }

    public LoginUser(SysUser sysUser) {
        this.sysUser = sysUser;
    }

    public LoginUser(SysUser sysUser, List<String> permissions) {
        this.sysUser = sysUser;
        this.permissions = permissions;
    }

    public LoginUser(SysUser sysUser, List<String> permissions, List<String> roles) {
       this(sysUser,permissions);
        this.roles = roles;
    }

    /**
     * 用于获取用户被授予的权限，可以用于实现访问控制。
     *
     * @return 授权用户列表
     */
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        // 将permissions集合中的String类型权限信息,转换为SimpleGrantedAuthority类型
        // List<SimpleGrantedAuthority> authorities = new ArrayList<>();
        // for (String permission : permissions) {
        //     SimpleGrantedAuthority simpleGrantedAuthority = new SimpleGrantedAuthority(permission);
        //     authorities.add(simpleGrantedAuthority);
        // }

        // 避免多次赋值,造成性能损耗
        if (this.authorities != null) {
            return this.authorities;
        }
        // 处理权限信息：1.8语法
        // this.authorities = this.permissions.stream().map(SimpleGrantedAuthority::new).collect(Collectors.toList());

        // 处理权限信息和角色信息，然后合并它们
        this.authorities = Stream.concat(
                this.permissions.stream().map(SimpleGrantedAuthority::new),
                this.roles.stream().map(role-> new SimpleGrantedAuthority("ROLE_"+role))
        ).collect(Collectors.toList());

        return this.authorities;
    }

    /**
     * 获取用户的密码
     *
     * @return 密码
     */
    @Override
    public String getPassword() {
        return sysUser.getPassword();
    }

    /**
     * 获取用户名,用于身份验证
     *
     * @return 用户名
     */
    @Override
    public String getUsername() {
        return sysUser.getUserName();
    }

    /**
     * 用于判断用户的账户是否未过期，可以用于实现账户有效期控制。
     */
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    /**
     * 用于判断用户的账户是否未锁定，可以用于实现账户锁定功能。
     */
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    /**
     * 用于判断用户的凭证（如密码）是否未过期，可以用于实现密码有效期控制。
     */
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    /**
     * 用于判断用户是否已激活，可以用于实现账户激活功能。
     */
    @Override
    public boolean isEnabled() {
        return true;
    }

    public SysUser getSysUser() {
        return sysUser;
    }

    public void setSysUser(SysUser sysUser) {
        this.sysUser = sysUser;
    }

    public List<String> getPermissions() {
        return permissions;
    }

    public void setPermissions(List<String> permissions) {
        this.permissions = permissions;
    }

    public void setAuthorities(List<SimpleGrantedAuthority> authorities) {
        this.authorities = authorities;
    }

    public List<String> getRoles() {
        return roles;
    }

    public void setRoles(List<String> roles) {
        this.roles = roles;
    }
}
